package flc.ast.activity;

import android.os.Bundle;
import android.os.Handler;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ZipUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ZipAdapter;
import flc.ast.bean.MyZipBean;
import flc.ast.databinding.ActivityAudioCompressBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class AudioCompressActivity extends BaseAc<ActivityAudioCompressBinding> {

    public static List<MyZipBean> listZip = new ArrayList<>();
    private int itemPos = 0;//当前正在处理的item
    private boolean isCompCom = true;//是否已经完成压缩
    private ZipAdapter zipAdapter;


    @Override
    protected int onCreate() {
        return R.layout.activity_audio_compress;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAudioCompressBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.rvZipList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        zipAdapter = new ZipAdapter();
        mDataBinding.rvZipList.setAdapter(zipAdapter);
        zipAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        zipAdapter.setList(listZip);
        compress(listZip.get(itemPos).getPath());
    }

    //压缩文件
    private void compress(String path) {
        RxUtil.create(new RxUtil.Callback<Boolean>() {
            @Override
            public void doBackground(ObservableEmitter<Boolean> emitter) {
                boolean isComp = false;
                mDataBinding.tvCurrentProgress.setText((itemPos + 1) + "/" + listZip.size());
                String resultFile = FileUtil.generateFilePath(Content.MyZip, ".zip");
                try {
                    isComp = ZipUtils.zipFile(path, resultFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                emitter.onNext(isComp);
            }

            @Override
            public void accept(Boolean isSuc) {
                zipAdapter.getItem(itemPos).setProgress(getRandom(90, 60));
                zipAdapter.notifyItemChanged(itemPos);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        zipAdapter.getItem(itemPos).setProgress(100);
                        zipAdapter.notifyItemChanged(itemPos);
                        if (itemPos < zipAdapter.getValidData().size() - 1) {
                            //继续压缩下一个
                            isCompCom = false;
                            itemPos++;
                            compress(zipAdapter.getItem(itemPos).getPath());
                        } else {
                            //压缩完成
                            isCompCom = true;
                        }
                    }
                }, 1000);
            }
        });
    }

    //生成区间内随机数
    private int getRandom(int maxNum, int minNum) {
        Random random = new Random();
        int i = random.nextInt(maxNum) % (maxNum - minNum + 1) + minNum;
        return i;
    }

}